package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgInvestmentDataMapper;
import com.csun.dsms.mapper.HgInvestmentPartDataMapper;
import com.csun.dsms.model.huang.HgFixedAssetData;
import com.csun.dsms.model.huang.HgInvestmentData;
import com.csun.dsms.model.huang.HgInvestmentPartData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.service.HgInvestmentDataService;
import com.csun.dsms.service.HgInvestmentPartDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgInvestmentPartDataServiceImpl extends BaseService<HgInvestmentPartData>
        implements HgInvestmentPartDataService {

    @Resource
    private HgInvestmentPartDataMapper hgInvestmentPartDataMapper;
    
    @Override
    public List<HgInvestmentPartData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgInvestmentPartDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public Wrapper importInvestmentPartTableData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgInvestmentPartData hgInvestmentPartData = new HgInvestmentPartData();
                hgInvestmentPartData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgInvestmentPartData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgInvestmentPartData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgInvestmentPartData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgInvestmentPartData.setTeamName(Convert.toStr(excelMap.get("专班名称")));
                hgInvestmentPartData.setOutgoingAttractionCumNum(Convert.toInt(excelMap.get("外出招商")));
                hgInvestmentPartData.setReceivedBusinessCumNum(Convert.toInt(excelMap.get("接待客商")));
                hgInvestmentPartData.setNegotiatingBusinessCumNum(Convert.toInt(excelMap.get("在谈项目")));
                hgInvestmentPartData.setSignedProjectCumNum(Convert.toInt(excelMap.get("签约项目")));
                hgInvestmentPartData.setAgreedInvestmentCumAmt(Convert.toDouble(excelMap.get("协议投资额")));
                hgInvestmentPartData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgInvestmentPartData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgInvestmentPartData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgInvestmentPartData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgInvestmentPartData.setCreatedTime(new Date());
                hgInvestmentPartDataMapper.insert(hgInvestmentPartData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入招商专班表格数据成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
